/******************************************************************************
*  COPYRIGHT MorningCore CO.,LTD
******************************************************************************/

/*----------- function definition-----------------------------------*/
/*****************************************************************************
* Function: PUSCH_QAMModu()
* Purpose:  This function performs pusch's modulation
*
* Relation:
Calls       : ....
Called by   : ....
* Params:
*   <Name>          <Type>      <In/Out>    <Description>
 P_DataIp[95040]    int8          In        input binary bit after scrambling
 PUSCH_RB_Num       int8          In        RB number of PUSCH
 PUSCH_SymNum       int8          In        number of SC-FDMA symbols for PUSCH
 ModuMod            int8          In        modulation mode
 DataOp[12][1320]   cmplx_frN     Out       output of modulation

* Return:  void
* Note:    <no>
*******************************************************************************/
#include "../head/ue_tx_pusch_scrambling_fix.h"
#include "../head/Ue_Tx_FilePrintf.h"

//#define _CRT_SECURE_NO_DEPRECATE
/******************************************************************************
*  INCLUDE FILES
******************************************************************************/
/*----------- function definition-----------------------------------*/
/*****************************************************************************
* Function: PUSCHScrambling()
* Purpose:  This function performs pusch bit scrambing
*
* Relation:
Calls       : ....
Called by   : ....
* Params:
*   <Name>          <Type>      <In/Out>    <Description>
  P_DataIp[95040]   int8           In         input binary bit
  PUSCH_BitLen      int32          In         input bit length
  CiniForPUSCH      int32          In         initialize for scrambling seq
  P_DataOp[95040]   int8           Out        output binary bit

* Return:  void
* Note:    <no>
*******************************************************************************/
void PUSCHScrambling_fix
(
    IN int8     P_DataIp[],
    IN int32    PUSCH_BitLen,
    IN int32    CiniForPUSCH,
    OUT int8    P_DataOp[],
    IN stULParamTable *SimParam
)
{
	/*variables definition*/
	uint32	x1,x2;
	int8	c;
	int8	val;
	//int8	ScrmSeqForPrint[CW_MAX_LEN]; //added by cyp for scream seq print
	int32	i;	//for cycle
    int8 c_write_out[36]={0};
	int8 *ScrmSeqForPrint;

	ScrmSeqForPrint = (int8(*))calloc(CW_MAX_LEN, sizeof(int8)); //allocate space and value 0


    x1 = 1;
    x2 = CiniForPUSCH;

	val =(char)((x1&1)^((x1>>3)&1));
	x1 = x1 | (val<<31);
	val =(char)((x2&1) ^ ((x2>>1)&1) ^ ((x2>>2)&1) ^ ((x2>>3)&1));
	x2 = x2 | (val<<31);
	
	//update x1,x2 sequence and calc c init val:
	for (i=0; i<Nc; i++)
	{
		x1 = x1_update_usp_fix (x1);
		x2 = x2_update_usp_fix (x2);
	}	
	SimParam->scramb_cinit = x2;
	SimParam->scramb_cinit_v1 = x1;
	c =(char)(x1^x2) & 1;
	//scram input codeword bit:
	for (i=0; i<PUSCH_BitLen; i++)
	{
		P_DataOp[i] = P_DataIp[i] ^ c;
		ScrmSeqForPrint[i] = c;

		x1 = x1_update_usp_fix (x1);
		x2 = x2_update_usp_fix (x2);
		c =(char)(x1^x2) & 1;
	}
	if (SimParam->PUSCH_EnFlag == 1)
	{
      vFilePrintDataPnSeqPrint(SimParam,&ScrmSeqForPrint[0],PUSCH_BitLen);
	}
	free(ScrmSeqForPrint);

}


//x1(n+31) = ( x1(n+3) + x1(n) ) % 2
uint32 x1_update_usp_fix(uint32 x1)
{
    uint32 val;

    x1 = x1 >> 1;
    val = (x1 & 1) ^ ((x1 >> 3) & 1);
    x1 = x1 | (val << 31);

    return x1;
}

//x2(n+31) = ( x2(n+3) + x2(n+2) + x2(n+1) + x2(n) ) % 2
uint32 x2_update_usp_fix(uint32 x2)
{
    uint32 val;
    x2 = x2 >> 1;
    val = (x2 & 1) ^ ((x2 >> 1) & 1) ^ ((x2 >> 2) & 1) ^ ((x2 >> 3) & 1);
    x2 = x2 | (val << 31);

    return x2;
}




